package me.ezjs.framework.core.model;

import java.io.*;

/**
 * 分页基本类,继承自DAOFilter,另外还包含有分页的相关信息
 *
 * @author zhujs
 */
public class FlipFilter extends DAOFilter {

    /**
     * 每页显示记录数,默认为15,最大10000.
     */
    public static final int DEFAULT_PAGESIZE = 15;
    public static final int MAX_PAGESIZE = 10000;

    /**
     * 当前页,默认为第一页
     */
    private int pageNo = 1;

    /**
     * 每页显示记录数
     */
    private int pageSize = DEFAULT_PAGESIZE;

    /**
     * 此次查询的总条数
     */
    private int totalCount = 0;

    private int totalPage = 1;

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    private int pageStart = 1;
    private int pageEnd = 1;

    public int getPageStart() {
        return pageStart;
    }

    public void setPageStart(int pageStart) {
        this.pageStart = pageStart;
    }

    public int getPageEnd() {
        return pageEnd;
    }

    public void setPageEnd(int pageEnd) {
        this.pageEnd = pageEnd;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    /**
     * 分页参数初始化.在调用该方法前,必须设定的参数如下:
     * <p/>
     * pageNo,totalCount,pageSize
     * <p/>
     * 初始化如下参数:
     * totalPage,
     * pageStart,
     * pageEnd,
     * pageNo
     */
    public void initFlipInfo() {
        totalPage = totalCount / pageSize;
        if (totalCount % pageSize > 0) {
            totalPage++;
        }

        this.pageStart = pageNo - 3 >= 1 ? pageNo - 3 : 1;

        pageEnd = pageNo + 3 <= totalPage ? pageNo + 3 : totalPage;
        if (pageNo > pageEnd) {
            pageNo = pageEnd;
        }
    }

    public void initFlipInfo(int pageNo, int totalCount) {
        this.pageNo = pageNo;
        this.totalCount = totalCount;
        initFlipInfo();
    }

    @Override
    public FlipFilter deepClone() {
        try {
            // 将对象写到流里
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo;
            oo = new ObjectOutputStream(bo);
            oo.writeObject(this);
            // 从流里读出来
            ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
            ObjectInputStream oi = new ObjectInputStream(bi);
            return (FlipFilter) (oi.readObject());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
